package com.umeox.iqibla.custom_tasbih

import com.umeox.moto.common.log.Logger
import com.umeox.moto.common.utils.DateUtils
import com.umeox.watch.moto.dataservice.DataProvider
import java.util.Calendar
import java.util.regex.Pattern

object Utils {
    private const val ALARM_EVERYDAY_REPEAT_EXP: String = "1111111"

    fun getAlarmList(): MutableList<TaskDetail> {
        val list: ArrayList<TaskDetail> = ArrayList()
        val alarms = DataProvider.getTasks()
        if (alarms != null && alarms.isNotEmpty()) {
            for (temp in alarms) {
                val task: TaskDetail? = TaskDetail.convertFromValues(temp)
                if (task != null) {
                    task.alarmTimeInMillis = getNextAlarmTimeMillis(task)
                    list.add(task)
                }
            }
        }
        Logger.i("获取任务：$list")
        return list
    }

    fun getNextAlarmDetail(taskDataList: MutableList<TaskDetail>): TaskDetail? {
        if (taskDataList.isEmpty()) {
            Logger.i("任务闹钟列表为空")
            return null
        }
        val temp: MutableList<TaskDetail> = ArrayList()
        for (detail in taskDataList) {
            if (detail.getAlarmTimeInMillis() > System.currentTimeMillis()) {
                temp.add(detail)
            }
        }
        var nextAlarm: TaskDetail? = null
        for (detail in temp) {
            Logger.i("每个任务闹钟，重复周期中距离现在最近的时间：" + DateUtils.formatDateTime(detail.getAlarmTimeInMillis()))
            if (nextAlarm == null
                || nextAlarm.getAlarmTimeInMillis() > detail.getAlarmTimeInMillis()
            ) {
                nextAlarm = detail
            }
        }
        return nextAlarm
    }

    private fun getNextAlarmTimeMillis(detail: TaskDetail): Long {
        val repeatExpression: String = detail.repeatExpression

        if (!isInvalidRepeatExpression(repeatExpression)) {
            return -1
        }
        val cal = Calendar.getInstance()
        cal[Calendar.HOUR_OF_DAY] = detail.hour
        cal[Calendar.MINUTE] = detail.minute
        cal[Calendar.SECOND] = 0
        cal[Calendar.MILLISECOND] = 0

        val timeInMillis = cal.timeInMillis
        val currentDayOfWeek = cal[Calendar.DAY_OF_WEEK] - 1
        val weeks = repeatExpression.toCharArray()
        if (weeks[currentDayOfWeek] == '1'
            && timeInMillis > System.currentTimeMillis()
        ) {
            return timeInMillis
        }

        if (repeatExpression == ALARM_EVERYDAY_REPEAT_EXP) {
            cal.add(Calendar.DAY_OF_MONTH, 1)
            return cal.timeInMillis
        }

        var temp = 7
        for (i in weeks.indices) {
            if (weeks[i] == '1') {
                var j = i - currentDayOfWeek
                if (j <= 0) {
                    j += 7
                }
                if (temp > j) {
                    temp = j
                }
            }
        }
        cal.add(Calendar.DAY_OF_MONTH, temp)
        return cal.timeInMillis
    }

    /**
     * 判断时间段，重复周期格式是否正确
     * 正确格式：1100011  7个字节长度，只允许0或者1
     */
    private fun isInvalidRepeatExpression(repeatExpression: String): Boolean {
        val rexp = "[0-1]{7}"
        return Pattern.compile(rexp).matcher(repeatExpression).matches()
    }

}